package com.sakura.mall.coupon.spuBounds.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.spuBounds.domain.entity.SpuBoundsEntity;
import com.sakura.mall.coupon.spuBounds.domain.request.AddSpuBoundsRequest;
import com.sakura.mall.coupon.spuBounds.domain.request.UpdateSpuBoundsRequest;
import com.sakura.mall.coupon.spuBounds.domain.request.SpuBoundsPageRequest;
import com.sakura.mall.coupon.spuBounds.domain.vo.SpuBoundsVo;
import com.sakura.mall.coupon.spuBounds.mapper.SpuBoundsMapper;
import com.sakura.mall.coupon.spuBounds.service.SpuBoundsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品spu积分设置 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SpuBoundsServiceImpl extends ServiceImpl<SpuBoundsMapper, SpuBoundsEntity> implements SpuBoundsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSpuBoundsRequest request) {
        // todo 其他校验逻辑
        SpuBoundsEntity spuBounds = request.convert(SpuBoundsEntity.class);
        this.save(spuBounds);
    }

    @Override
    public Page<SpuBoundsVo> queryPage(SpuBoundsPageRequest request) {
        IPage<SpuBoundsEntity> qp = request.toPage();
        LambdaQueryWrapper<SpuBoundsEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SpuBoundsVo> ret = qp.convert(entity -> entity.convert(SpuBoundsVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSpuBoundsRequest request) {
        SpuBoundsEntity entity = request.convert(SpuBoundsEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

